﻿/* 
 * Copyright (c) 2011 Jeff Zhang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, 
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Text;
using System.Reflection;
using System.Runtime.Serialization;

namespace Json {
    public abstract class JsonObject<T> : IJsonObject, IJsonObject<T> {
        private T _value;

        protected JsonObject(T value) {
            _value = value;
        }

        public abstract DataType Type { get; }

        public T Value {
            get { return _value; }
        }

        public abstract int Length { get; }

        object IJsonObject.Value {
            get { return _value; }
        }

        public abstract IJsonObject this[string key] { get; set; }

        public abstract IJsonObject this[int index] { get; set; }

        protected virtual string MakeBlank(int count) {
            if (count <= 0)
                return "";

            char[] chr = new char[count * 4];

            return new string(chr);
        }

        public abstract string ToJson();

        public override string ToString() {
            return ToJson();
        }

        public abstract byte[] ToBytes();
    }

    #region JsonObject

    public sealed class JsonObject : JsonObject<IJsonObject> {
        public readonly static JsonNull Null = new JsonNull();
        public readonly static JsonArray EmptyArray = new JsonArray(0);
        public readonly static JsonDictionary EmptyObject = new JsonDictionary(0);

        public JsonObject() : base(JsonObject.Null) { }

        public JsonObject(IJsonObject json) : base(json) { }

        public override DataType Type {
            get { return DataType.Object; }
        }

        public override IJsonObject this[int index] {
            get {
                if (Value is JsonArray) {
                    return Value[index];
                }

                throw new IllegalOperationException();
            }

            set {
                if (Value is JsonArray) {
                    Value[index] = value;
                } else {
                    throw new IllegalOperationException();
                }
            }
        }

        public override IJsonObject this[string key] {
            get {
                if (Value is JsonDictionary) {
                    return Value[key];
                }

                throw new IllegalOperationException();
            }
            set {
                if (Value is JsonDictionary) {
                    Value[key] = value;
                } else {
                    throw new IllegalOperationException();
                }
            }
        }

        public static IJsonObject Parser(object obj, Type type) {
            if (obj == null)
                return JsonObject.Null;

            PropertyInfo[] fields = type.GetProperties();
            JsonDictionary json = new JsonDictionary();

            foreach (PropertyInfo field in fields) {
                foreach (Attribute attr in field.GetCustomAttributes(typeof(JsonAttribute), false)) {
                    JsonAttribute attribute = attr as JsonAttribute;

                    if (attribute != null) {
                        json[attribute.Name] = attribute.ToJsonObject(field.GetValue(obj, null));
                    }
                }
            }

            return json;
        }

        public static IJsonObject Parser(object obj) {
            if (obj == null)
                return JsonObject.Null;

            return Parser(obj, obj.GetType());
        }

        public override int Length {
            get {
                return Value.Length;
            }
        }

        public override string ToJson() {
            return Value.ToJson();
        }
    }

    #endregion
}
